import { encode } from "../assets/js/base64.js";
import { hasThisButton } from "../assets/js/utils.js";
// 定位搜索模块表单宽度
const setwidth = {
  inserted: function(el, binding, vnode) {
    setTimeout(() => {
      try {
        const parent = el.parentNode;
        const prev = parent.previousSibling
          ? parent.previousSibling.offsetWidth
          : 0;
        // console.log(parent.parentNode.offsetWidth);
        // console.log(parent.previousSibling.offsetWidth);
        el.style.width = parent.parentNode.offsetWidth - prev + "px";
      } catch (error) {}
    }, 0);
    setTimeout(() => {
      try {
        const parent = el.parentNode;
        const prev = parent.previousSibling
          ? parent.previousSibling.offsetWidth
          : 0;
        // console.log(parent.parentNode.offsetWidth);
        // console.log(parent.previousSibling.offsetWidth);
        el.style.width = parent.parentNode.offsetWidth - prev + "px";
      } catch (error) {}
    }, 300);
  }
};

// 根据权限控制显示隐藏
const limit = {
  inserted(el, binding, vnode) {
    let parentNode = el.parentNode;
    let div = document.createElement("div");
    div.style.display = "none";
    div.setAttribute("limit", encode(binding.value));
    try {
      // console.log(binding.value);
      if (binding.value) {
        let limit = hasThisButton(binding.value);
        if (!parentNode) {
          return;
        }
        if (!limit) {
          // console.log(limit);
          parentNode.replaceChild(div, el);
          // parentNode.removeChild(el);
        } else {
          div = parentNode.getElementsByName(encode(binding.value));
          parentNode.replaceChild(el, div);
        }
      }
    } catch (error) {}
    window.addEventListener("changeLimit", event => {
      // console.log(event);
      // console.log(parentNode);
      // console.log(parentNode.querySelector(`[limit=${encode(binding.value)}]`));
      try {
        // console.log(binding.value);
        if (binding.value) {
          let limit = hasThisButton(binding.value);
          if (!parentNode) {
            return;
          }
          if (!limit) {
            parentNode.replaceChild(div, el);
            // parentNode.removeChild(el);
          } else {
            div = parentNode.querySelector(`[limit=${encode(binding.value)}]`);
            // console.log(div);
            // console.log(el);
            parentNode.replaceChild(el, div);
          }
        }
      } catch (error) {}
    });
  }
};
export default {
  setwidth,
  limit
};
